

clear all	
cd "${data}"	
use "CMP_original.dta", clear

nsplit date, digits(4 2) generate(year month)

tostring edate, generate(electiondate)
drop if country==74 & edate==20393
drop if country==34 & edate==20351
drop if country==34 & edate==19119

gen rile_rs = ( rile - (-100) ) / ( (100)-(-100) ) * 10


* log scale for rile 

gen rilelog = log(per104 + per201 + per203 + per305 + per401 + per402 + per407 + per414 + per505 + per601 + per603 + per605+ per606 + 0.5) - log(per103 + per105 + per106 + per107 + per403 + per404 + per406 + per412 + per413 + per504 + per506 + per701 + per202 + 0.5) 

gen rilelog_rs = (rilelog - (-5.007965) ) / ( (5.219188)-(-5.007965) ) * 10


gen rilelog_alt = log(per104 + per201 + per203 + per305 + per401 + per402 + per407 + per414 + per505 + per601 + per603 + per605+ per606 + 0.5) / log(per103 + per105 + per106 + per107 + per403 + per404 + per406 + per412 + per413 + per504 + per506 + per701 + per202 + 0.5) 


rename countryname CMP_countryname
rename year CMP_year
rename party CMP_party
rename partyname CMP_partyname
rename pervote CMP_pervote


keep CMP_countryname CMP_year rile CMP_party CMP_partyname CMP_pervote rilelog rilelog_rs rilelog_alt parfam

drop if CMP_year==.
drop if CMP_year<1990
drop if CMP_year>2021
gen CMP_key = string(CMP_party)+"_"+string(CMP_year) // THAT'S THE KEY TO CONNECT TO CSES 



* ATTENTION! "party" is the key variable for merging 


save "CMP_coded.dta", replace

use "d2.dta", clear 

gen CMP_key = string(IMD5100_)+"_"+string(IMD1008_YEAR) 

*drop if IMD5100_==.
*drop if CMP_key == "._." | CMP_key== "._1996" | CMP_key== "._1999" | CMP_key== "._2002" | CMP_key== "._2004" | CMP_key== "._2005" | CMP_key== "._2006" | CMP_key== "._2007" | CMP_key== "._2008" | CMP_key== "._2009" | CMP_key== "._2013" | CMP_key== "._2014" | CMP_key== "._2017" | CMP_key== "._2018" | CMP_key== "._2019" | CMP_key== "._2020" | CMP_key== "._2021" 
drop if respondent==.

merge m:m CMP_key using "CMP_coded.dta" 

*keep if _merge==3

label variable daysafter "days since election"
label variable IMD5012_ "party position"
label variable election "election"
label variable country "country"
label variable respondent "respondent"
label variable rile "party position CMP"
label variable rile "party position CMP (log)"
label variable CHES_leftright "party position CHES"

drop if daysafter==.

save "data1.dta", replace 

